#include "pub.h"
#include <routes_api.h>
#include <stdio.h>
// #include <string.h>

typedef struct routesTbl {
    RoutesCallbackFunc map[E_ROUTES_CB_INVALID];
    // BEST_UINT32        regFuncCnt;
} RoutesTbl;

RoutesTbl g_RoutesFuncTbl = {0};

BEST_STATUS RoutesReceive(ROUTES_MSG_TYPE typeCode, BEST_UINT32 *msgHdr,
                          BEST_UINT32 msgLen)
{
    RoutesCallbackFunc fn = g_RoutesFuncTbl.map[typeCode];
    if (fn == NULL) {
        printf("routesCallbackfunc is err!\n");
        return BEST_ERR;
    }

    return fn(msgHdr, msgLen);
}

BEST_STATUS RoutesInit()
{
    // memset(&g_RoutesFuncTbl, 0, sizeof(g_RoutesFuncTbl));
    BEST_INT32 i = 0;
    for (i = 0; i < E_ROUTES_CB_INVALID; i++) {
        g_RoutesFuncTbl.map[i] = NULL;
    }
    return BEST_OK;
}
BEST_STATUS RoutesSendMsg(ROUTES_MSG_TYPE typeCode, BEST_UINT32 *msgHdr,
                          BEST_UINT32 msgLen)
{
    if (typeCode > E_ROUTES_CB_INVALID) {
        return BEST_ERR;
    }
    return RoutesReceive(typeCode, msgHdr, msgLen);
    // return BEST_OK;
}

BEST_STATUS RoutesRegMsgCallback(ROUTES_MSG_TYPE    typeCode,
                                 RoutesCallbackFunc fn)
{
    if (typeCode >= E_ROUTES_CB_INVALID) {
        return BEST_ERR;
    }
    if (g_RoutesFuncTbl.map[typeCode] != NULL) {
        printf("reg duplicate\n");
    }
    g_RoutesFuncTbl.map[typeCode] = fn;

    // g_RoutesFuncTbl.regFuncCnt++;
    return BEST_OK;
}

BEST_STATUS RoutesUnRegMsgCallback(ROUTES_MSG_TYPE typeCode)
{
    if (typeCode >= E_ROUTES_CB_INVALID) {
        return BEST_ERR;
    }
    g_RoutesFuncTbl.map[typeCode] = NULL;
    // BEST_UINT32 regFuncCnt     = g_RoutesFuncTbl.regFuncCnt;
    // g_RoutesFuncTbl.regFuncCnt = regFuncCnt > 0 ? regFuncCnt - 1 : 0;
    return BEST_OK;
}
